package com.hn658.base.message.appinfo.dao.redis.impl;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;

import com.hn658.base.message.appinfo.dao.redis.IAppMqRedisDAO;
import com.hn658.base.message.appinfo.entity.AppMqConfigEO;
import com.hn658.base.message.common.utils.RedisKeyHelper;
import com.hn658.framework.dataaccess.redis.AbstractRedisDAO;
import com.hn658.framework.shared.utils.JsonMapper;

/**
 * 应用Mq Redis DAO 实现类
 */
@Repository
public class AppMqRedisDAOImpl extends AbstractRedisDAO<AppMqConfigEO>
		implements IAppMqRedisDAO {
	@Autowired
	@Qualifier("redisTemplate")
	@Override
	public void setTemplate(StringRedisTemplate template) {
		super.template = template;
	};
	
	JsonMapper jsonMapper = JsonMapper.nonEmptyMapper();
	
	@Override
	public AppMqConfigEO findAppMqById(Long id) {
		if(id==null){
			return null;
		}
		String appInfoJson = valueOps.get(RedisKeyHelper.appMqId(id.toString()));
		AppMqConfigEO appMq = jsonMapper.fromJson(appInfoJson, AppMqConfigEO.class);
        return appMq;
	}

	@Override
	public AppMqConfigEO findAppMqByNum(String queueNumber) {
		// 编号对应的Mq id
		String id = findIdByNum(queueNumber);
		if(StringUtils.isEmpty(id)){
			return null;
		}

		// 从Redis中获取对应id的Mq
		return this.findAppMqById(Long.valueOf(id));
	}

	@Override
	public void saveAppMq(AppMqConfigEO appMq) {
		if(appMq == null){
			return;
		}
		
        if (StringUtils.isNotBlank(appMq.getId().toString())) {
            // 设置appId对应的id
            valueOps.set(RedisKeyHelper.appMqNum(appMq.getQueueNumber().toString()),
            		appMq.getId().toString());
            String appInfoJson = jsonMapper.toJson(appMq);
            valueOps.set(RedisKeyHelper.appMqId(appMq.getId().toString()), appInfoJson);
        }
        
	}
	
	/**
	 * 根据编号查找对应的id
	 * @param number
	 * @return
	 */
	private String findIdByNum(String number) {
		return valueOps.get(RedisKeyHelper.appMqNum(number));
	}

	/**
	 * 
	 * <p>删除关联的redis数据</p> 
	 * @param id 
	 */
	@Override
	public void deleteAppMq(Long id) {
		this.template.delete(RedisKeyHelper.appMqId(id.toString()));
		AppMqConfigEO appInfo = this.findAppMqById(id);
		if(appInfo!=null){
			this.template.delete(RedisKeyHelper.appMqNum(appInfo.getQueueNumber().toString()));			
		}
	}
	@Override
	public String createQueueNumber(String redisKey,String prefix){
        SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");
        Date now = new Date();
        Calendar nowCal = Calendar.getInstance();
        nowCal.setTime(now);
        long id = valueOps.increment(redisKey, 1);
        //设置缓存数据最后的失效时间为当天的最后一秒
        if(id==1L){
            nowCal = Calendar.getInstance();
            nowCal.setTime(now);
            Calendar lastCal = Calendar.getInstance();
            lastCal.set(nowCal.get(Calendar.YEAR), nowCal.get(Calendar.MONTH), nowCal.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
            lastCal.set(Calendar.MILLISECOND, 999);
            template.expireAt(redisKey, lastCal.getTime());
        }
        String orderId = StringUtils.leftPad(String.valueOf(id), 7, '0');
        StringBuffer sb = new StringBuffer(prefix);
        sb.append(sdf.format(nowCal.getTime())).append(orderId);
        return sb.toString();
    }
}
